package com.galengao.springboot.validateAndResponse.eneity;

import com.galengao.springboot.validateAndResponse.BizException;
import com.galengao.springboot.validateAndResponse.resp.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;

/**
 * 控制器异常处理
 *
 * @author GalenGao
 * @version Id: ControllerExceptionHandler.java, v 0.1 2022/6/2 14:11 GalenGao Exp $$
 */
@RestControllerAdvice(annotations = RestControllerAdvice.class)
@Slf4j
public class ControllerExceptionHandler {

    @ExceptionHandler(value = {Exception.class, RuntimeException.class})
    @ResponseBody
    public BaseResponse<Void> handle(Throwable e, HandlerMethod handlerMethod) {
        // bean 名称
        String beanName = handlerMethod.getBeanType().getName();
        // 方法名称
        String methodName = handlerMethod.getMethod().getName();
        if(e instanceof BizException) {
            return BaseResponse.fail("业务异常",403);
        }
        return null;
    };
}
